package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.LoginException;
import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.mapper.zjs.AccountCouponsMapper;
import com.fantastic.mapper.zjs.AccountMapper;
import com.fantastic.mapper.zjs.UserMapper;
import com.fantastic.model.pojo.zjs.Account;
import com.fantastic.model.pojo.zjs.AccountCoupons;
import com.fantastic.model.vo.zjs.AccountCouponsVo;
import com.fantastic.model.vo.zjs.UserVo;
import com.fantastic.service.zjs.AuthService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: rxx
 * @since: 2024/6/14 09:57
 * @description: 客户端身份验证
 */
@Service("appAuth")
public class AuthServiceAppImpl implements AuthService {

    @Resource
    private AccountMapper accountMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private AccountCouponsMapper accountCouponsMapper;

    /**
     * 登录
     *
     * @param account 账号对象
     * @return 用户信息
     */
    @Override
    public UserVo login(Account account) {
        //判断账号密码或是手机号是否为空
        IsEmptyUtils.stringIsEmpty(new SelectException(RespStatusInfo.PHONE_NULL), account.getPhone());
        Account accounts = accountMapper.loginAccount(account);
        //判断账号是否存在
        if (accounts != null) {
            if (accounts.getAccountId() != null) {
                //判断账号状态
                if (accounts.getAccountStatus() != 0) {
                    //返回用户信息
                    return getUserVo(accounts);
                }
                //抛出账号封禁
                throw new LoginException(RespStatusInfo.ACCOUNT_ERROR);
            }
        }
        //抛出登录失败异常
        throw new LoginException(RespStatusInfo.PHONE_ISNULL);
    }

    /**
     * 封装UserVo信息
     *
     * @param account
     * @return
     */
    public UserVo getUserVo(Account account) {
        UserVo userVo = new UserVo();
        //设置用户基本信息
        userVo.setUser(userMapper.getUser(account.getUserId()));
        //通过手机号拿到完整的账号信息
        Account getAccount = accountMapper.getAccount(account.getAccountId());

        //设置用户手机号和余额
        userVo.setPhone(getAccount.getPhone());
        userVo.setAccountStored(getAccount.getAccountStored());
        userVo.setAccountId(getAccount.getAccountId());
        //设置用户所持卷数量
        AccountCoupons accountCoupons = new AccountCoupons();
        accountCoupons.setAccountId(getAccount.getAccountId());

        AccountCouponsVo accountCouponsList = accountCouponsMapper.getAccountCoupons(getAccount.getAccountId());

        if (accountCouponsList.getCoupons().size() == 0) {
            userVo.setCouponsCount(0);
        } else {
            userVo.setCouponsCount(accountCouponsList.getCoupons().size());
        }

        userVo.getUser().setUserPicture(FilesUtils.getSlashParse(userVo.getUser().getUserPicture()));
        return userVo;
    }


}
